//
//  ViewController.swift
//  BasicAnimation-Swift
//
//  Created by Stan on 2017-02-09.
//  Copyright © 2017 stan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        redLayer.add(createBasicAnimation(fromValue: CGPoint(x: 0, y: 150), toValue: CGPoint(x: 300, y: 150), timingFunction: kCAMediaTimingFunctionLinear), forKey: "linear")
        
        blueLayer.add(createBasicAnimation(fromValue: CGPoint(x: 0, y: 250), toValue: CGPoint(x: 300, y: 250), timingFunction: kCAMediaTimingFunctionEaseIn), forKey: "linear")
        
        grayLayer.add(createBasicAnimation(fromValue: CGPoint(x: 0, y: 350), toValue: CGPoint(x: 300, y: 350), timingFunction: kCAMediaTimingFunctionEaseOut), forKey: "linear")
        
        greenLayer.add(createBasicAnimation(fromValue: CGPoint(x: 0, y: 450), toValue: CGPoint(x: 300, y: 450), timingFunction: kCAMediaTimingFunctionEaseInEaseOut), forKey: "linear")
        
    }
    
    

    
    //MARK: - 懒加载
    private lazy var redLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 0, y: 150), backgroundColor: UIColor.red)
        
        return layer
    }()
    
    private lazy var blueLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 0, y: 250), backgroundColor: UIColor.blue)
        
        return layer
    }()
    
    private lazy var grayLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 0, y: 350), backgroundColor: UIColor.gray)
        
        return layer
    }()
    
    private lazy var greenLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 0, y: 450), backgroundColor: UIColor.green)
        
        return layer
    }()
    
    
}

//MARK: - 抽取创建动画及Layer的公共方法
extension ViewController: CAAnimationDelegate{
    
    //    创建calayer
    fileprivate func createLayer (position: CGPoint, backgroundColor: UIColor) -> CALayer {
        let layer = CALayer()
        layer.position = position
        layer.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        
        layer.backgroundColor = backgroundColor.cgColor
        
        self.view.layer.addSublayer(layer)
        
        return layer
    }
    
    
    
    //创建basicAnimation
    fileprivate func createBasicAnimation (fromValue: CGPoint, toValue: CGPoint, timingFunction: String) -> CABasicAnimation {
        //创建动画对象
        let basicAni = CABasicAnimation()
        //设置动画属性
        basicAni.keyPath = "position"
        
        //设置动画的起始位置。也就是动画从哪里到哪里
        basicAni.fromValue = fromValue
        
        //动画结束后，layer所在的位置
        basicAni.toValue = toValue
        
        //动画持续时间
        basicAni.duration = 2;
        
        //动画重复次数
        basicAni.repeatCount = Float(CGFloat.greatestFiniteMagnitude)
        
        //xcode8.0之后需要遵守代理协议
        basicAni.delegate = self;
        
        basicAni.timingFunction = CAMediaTimingFunction.init(name: timingFunction)
        
        return basicAni;
    }
    
    
}

















